package com.fpt.quanlyhethongsan.controller;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.fpt.quanlyhethongsan.utils.Constants;
import com.fpt.quanlysanbong.dao.SoccerFieldDao;
import com.fpt.quanlysanbong.domain.Account;
import com.fpt.quanlysanbong.domain.Person;
import com.fpt.quanlysanbong.domain.SoccerField;
import com.fpt.quanlysanbong.domain.TimeDivision;

public class SoccerFieldController {

	/** The id soccer field. */
	private int id;

	/** The name soccer field. */
	private String name;

	/** The image soccer field. */
	private String image_url;

	/** The desccription. */
	private String description;

	/** The owner. */
	private Person owner;

	/** The staff. */
	private Person staff;

	/** The status. */
	private boolean status;

	/** The delete. */
	private boolean delete;

	/** The address. */
	private String address;

	private String district;

	private String operation;

	private List<String> period1;
	private List<String> price1;
	private List<String> period2;
	private List<String> price2;
	private List<String> period3;
	private List<String> price3;
	private List<String> period4;
	private List<String> price4;
	private List<String> period5;
	private List<String> price5;
	private List<String> subSoccerField;
	private List<String> subSoccerFieldId;

	private String keyword;

	private SoccerField soccerField;

	private boolean isSuccess;

	private List<SoccerField> listSoccerField;

	private HttpServletRequest request = ServletActionContext.getRequest();

	public List<String> getSubSoccerFieldId() {
		return subSoccerFieldId;
	}

	public void setSubSoccerFieldId(List<String> subSoccerFieldId) {
		this.subSoccerFieldId = subSoccerFieldId;
	}

	public List<String> getPrice1() {
		return price1;
	}

	public void setPrice1(List<String> price1) {
		this.price1 = price1;
	}

	public List<String> getPeriod2() {
		return period2;
	}

	public void setPeriod2(List<String> period2) {
		this.period2 = period2;
	}

	public List<String> getPrice2() {
		return price2;
	}

	public void setPrice2(List<String> price2) {
		this.price2 = price2;
	}

	public List<String> getPeriod3() {
		return period3;
	}

	public void setPeriod3(List<String> period3) {
		this.period3 = period3;
	}

	public List<String> getPrice3() {
		return price3;
	}

	public void setPrice3(List<String> price3) {
		this.price3 = price3;
	}

	public List<String> getPeriod4() {
		return period4;
	}

	public void setPeriod4(List<String> period4) {
		this.period4 = period4;
	}

	public List<String> getPrice4() {
		return price4;
	}

	public void setPrice4(List<String> price4) {
		this.price4 = price4;
	}

	public List<String> getPeriod5() {
		return period5;
	}

	public void setPeriod5(List<String> period5) {
		this.period5 = period5;
	}

	public List<String> getPrice5() {
		return price5;
	}

	public void setPrice5(List<String> price5) {
		this.price5 = price5;
	}

	public List<String> getSubSoccerField() {
		return subSoccerField;
	}

	public void setSubSoccerField(List<String> subSoccerField) {
		this.subSoccerField = subSoccerField;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public List<SoccerField> getListSoccerField() {
		return listSoccerField;
	}

	public void setListSoccerField(List<SoccerField> listSoccerField) {
		this.listSoccerField = listSoccerField;
	}

	public boolean isSuccess() {
		return isSuccess;
	}

	public void setSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getDistrict() {
		return district;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	public SoccerField getSoccerField() {
		return soccerField;
	}

	public void setSoccerField(SoccerField soccerField) {
		this.soccerField = soccerField;
	}

	public List<String> getPeriod1() {
		return period1;
	}

	public void setPeriod1(List<String> period1) {
		this.period1 = period1;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getImage_url() {
		return image_url;
	}

	public void setImage_url(String image_url) {
		this.image_url = image_url;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Person getOwner() {
		return owner;
	}

	public void setOwner(Person owner) {
		this.owner = owner;
	}

	public Person getStaff() {
		return staff;
	}

	public void setStaff(Person staff) {
		this.staff = staff;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public boolean isDelete() {
		return delete;
	}

	public void setDelete(boolean delete) {
		this.delete = delete;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public String getPriceFormat(float number) {
		DecimalFormat f = new DecimalFormat("#,###,###");
		return f.format(number);
	}

	public String getPrice(float number) {
		DecimalFormat f = new DecimalFormat("###");
		return f.format(number);
	}

	// return Register SoccerField
	public String registerSoccerField() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		setOperation(Constants.ADD);
		return Constants.SUCCESS;
	}

	public String saveSoccerField() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		Account acc = (Account) session.getAttribute(Constants.USER_SESSION);

		List<TimeDivision> timeDivisions = new ArrayList<TimeDivision>();

		for (int i = 0; i < subSoccerField.size(); i++) {
			timeDivisions.add(new TimeDivision(subSoccerField.get(i), period1.get(i), Float.parseFloat(price1.get(i))));
			timeDivisions.add(new TimeDivision(subSoccerField.get(i), period2.get(i), Float.parseFloat(price2.get(i))));
			timeDivisions.add(new TimeDivision(subSoccerField.get(i), period3.get(i), Float.parseFloat(price3.get(i))));
			timeDivisions.add(new TimeDivision(subSoccerField.get(i), period4.get(i), Float.parseFloat(price4.get(i))));
			timeDivisions.add(new TimeDivision(subSoccerField.get(i), period5.get(i), Float.parseFloat(price5.get(i))));
		}

		soccerField = new SoccerField();

		soccerField.setAddress(address);
		soccerField.setDelete(false);
		soccerField.setDescription(description);
		soccerField.setDistrict(district);
		image_url = getAndCreateFile().trim();
		soccerField.setImage_url(image_url);
		soccerField.setName(name);
		soccerField.setOwner(acc.getPerson());
		soccerField.setStaff(null);
		soccerField.setStatus(false);
		soccerField.setListTimeDivision(timeDivisions);

		if (StringUtils.equalsIgnoreCase(operation, Constants.ADD)) {
			isSuccess = new SoccerFieldDao().addSoccerField(soccerField);
		} else if (StringUtils.equalsIgnoreCase(operation, Constants.UPDATE)) {
			soccerField.setId(id);
			isSuccess = new SoccerFieldDao().editSoccerField(soccerField);
		}

		if (isSuccess)
			return Constants.SUCCESS;
		else
			return Constants.ERROR;
	}

	public String listSoccerField() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		Account acc = (Account) session.getAttribute(Constants.USER_SESSION);
		listSoccerField = new SoccerFieldDao().getListSoccerField(acc.getPerson().getId());

		setOperation(Constants.UPDATE);
		return Constants.SUCCESS;
	}

	public String deleteSoccerField() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		isSuccess = new SoccerFieldDao().deleteSoccerField(id);
		if (isSuccess)
			return Constants.SUCCESS;
		else {
			return Constants.ERROR;
		}
	}

	public String soccerFieldAvaiable() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		listSoccerField = new SoccerFieldDao().getSoccerFieldAvaiable(null);
		return Constants.SUCCESS;
	}

	public String searchSoccerField() {

		listSoccerField = new SoccerFieldDao().getSoccerFieldAvaiable(keyword);
		return Constants.SUCCESS;
	}

	public String getSoccerFieldOfStaff() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute(Constants.USER_SESSION) == null)
			return Constants.ERROR;
		SoccerField sc = (SoccerField) session.getAttribute(Constants.CURRENT_FIELD);

		if (sc == null) {
			return Constants.ERROR;
		}

		listSoccerField = new ArrayList<SoccerField>();
		listSoccerField.add(sc);

		return Constants.SUCCESS;
	}

	public String getAndCreateFile() {
		String fileName = image_url.replace(request.getContextPath(), "");

		return fileName;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}

}
